package Math;

import java.util.ArrayList;
import java.util.List;

public class _889_SpiralMatrixIII {
    public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
        int[][] res = new int[R * C][2];
        int[] dr = new int[]{0, 1, 0, -1};
        int[] dc = new int[]{1, 0, -1, 0};
        int count = 0;
        res[count++] = new int[]{r0, c0};
        if(R*C==1) return res;
        for (int i = 1; i < 2*(R+C); i+=2) {
            for (int j = 0; j < 4; j++) {
                int steps = i + (j / 2);
                for (int k = 0; k < steps; k++) {
                    r0 += dr[j];
                    c0 += dc[j];
                    if (0 <= r0 && r0 < R && 0 <= c0 && c0 < C) {
                        res[count++] = new int[]{r0, c0};
                        if(count==R*C) return res;
                    }
                }
            }
        }
        throw null;
    }
}
